﻿using CPPEI;
using CPPEI.WebApi;
using SLG.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Ser = SLG.Domain.Services;
using Ent = SLG.Domain.Entities;
using V = SLG.Domain.Views;

namespace SLG.WebApi.Army
{
    [AuthGetAction("Army", "GetList", "1.0")]
    public class GetListAction : BaseAction<GetListParams, GetListResult>
    {
        protected override ActionResult<GetListResult> OnExecute(ActionContext context, SysParameters sysParams, GetListParams opParams)
        {
            int userId = Convert.ToInt32(context.UserId);

            Ser.QueryService service = new Ser.QueryService();
            ListResult<Ent.T_Army> serResult = service.QueryArmies(userId, opParams.CityId);

            if (serResult.IsSuccess)
            {
                GetListResult actResult = new GetListResult();

                for (int index = 0; index < serResult.Data.Count; index++)
                {
                    GetListResult.Item item = new GetListResult.Item();

                    item.Id = serResult.Data[index].Id;
                    item.KingdomId = serResult.Data[index].KingdomId;
                    item.CityId = serResult.Data[index].CityId;
                    item.MarshalId = serResult.Data[index].MarshalId;
                    item.FormationId = serResult.Data[index].FormationId;
                    item.Number = serResult.Data[index].Number;
                    item.PosX = serResult.Data[index].PosX;
                    item.PosY = serResult.Data[index].PosY;
                    actResult.Items.Add(item);
                }
                return new ActionResult<GetListResult>(actResult);
            }
            else
            {
                return new ActionResult<GetListResult>(serResult);
            }
        }
    }

    public class GetListParams
    {
        public Guid CityId { get; set; }
    }

    public class GetListResult
    {
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public Guid Id { get; set; }
            public Guid KingdomId { get; set; }
            public Guid CityId { get; set; }
            public Guid MarshalId { get; set; }
            public int FormationId { get; set; }
            public int Number { get; set; }
            public int PosX { get; set; }
            public int PosY { get; set; }
        }
    }
}
